// @charset "utf-8";

define(function(require, exports, module) {
	var publicMethod = require('../../public/public'),
		clientApi = require('../../public/clientapi');

	var IndexBase = require('../allindex/indexbase');

	function ZdtjModule() {
		this.domId = 'zdtj';
		this.MODULENAME = '涨跌统计';
		this.EXPLAINTEXT = '实时提示两市个股涨跌。涨幅大于+5%的数量对判断市场当日风向有重要作用，涨幅超过5%的数量越多，大盘越安全；反之需警惕市场风险。';

		this.type = 'min';

		// this.code = '883900,1A0001';
		// this.indexCode = '883900';
		this.hcType = 'zdtjpjhc';

		this.latestVal1 = '--';
		this.latestVal2 = '--';
		this.latestVal3 = '--';

		this.beginDate = '';

		this.sum = 0;
		this.HighchartsObj = null;
		this.renderDom();
	}

	publicMethod.extendClass(ZdtjModule, IndexBase);

	ZdtjModule.prototype.renderDom = function() {
		var self = this;
		var htmlDom = '<li data-mname="' + self.domId + '">' +
			'<div class="module-item">' +
			'<div class="module-header">' +
			'<a href="###" class="icon module-close-btn"></a>' +
			'<div class="date-wrap">' +
			'<p class="date-wrap-single">' +
			'<input type="text" class="icon date-picker" value="2017-02-16" id="' + self.domId + 'DatePicker" />' +
			// '<a href="###" class="icon interval-btn" title="切换区间" id="'+ self.domId + 'IntervalBtn"></a>' +
			'</p>' +
			// '<p class="date-wrap-interval hide">' +
			// 	'<input type="text" class="icon date-picker" value="2017-02-16" id="'+ self.domId + 'SDatePicker" />' +
			// 	'<i class="separator">-</i>' +
			// 	'<input type="text" class="icon date-picker" value="2017-02-16" id="'+ self.domId + 'EDatePicker"  />' +
			// 	'<a href="###" class="icon single-btn" title="切换区间" id="'+ self.domId + 'SingleBtn"></a>' +
			// '</p>' +
			'</div>' +
			'<p class="module-name">' + self.MODULENAME + '</p><i class="icon module-tip-btn" id="' + self.domId + 'ExplainBtn"><b class="icon"></b></i>' +
			'</div>' +
			'<div class="module-main">' +
			'<div class="chart-box" id="' + self.domId + 'ChartBox">' +
			'</div>' +
			'<div class="explain-box hide" id="' + self.domId + 'ExplainBox">' +
			'<p class="explain-text">' + self.EXPLAINTEXT + '</p>' +
			'<div class="clearfix">' +
			'<div class="explain-left ' + self.domId + '-explain-pic">' +
			'</div>' +
			'<div class="explain-right">' +
			'<p>今日解读：<span id="' + self.domId + 'ExplainData">放量净流入12亿</span></p>' +
			'<br/>' +
			'<p>上涨概率回测</p>' +
			'<p id="' + self.hcType + 'HcData">历史数据回测中...</p>' +
			'</div>' +
			'</div>' +
			'</div>' +
			'<p class="non-trade-tip" id="' + self.domId + 'NonTradeTip">非交易日，请重新选择</p>' +
			'</div>' +
			'</div>' +
			'</li>';
		$('#modules-list-ul').append(htmlDom);
		self.renderData();

		self.bindEvents();
	}
	ZdtjModule.prototype.renderData = async function() {
		var tradeDay = await clientApi.getTradingDay();

		this.requestMinData(tradeDay);

	}
	ZdtjModule.prototype.requestMinData = function(date) {
		var self = this;
		self.beginDate = date;

		var url = 'http://10.10.14.128:1080/test/chenyj/crm55265/index.php?market=USHA_USZA&datatype=zhangfustats_detail';
		publicMethod.ajaxJsonp({
			url: url,
			data: {
				date: date
			},
			jsonpCallback: 'zhangfustats',
			callback: function(result) {
				self.analyticalData(result);
			}
		});

		this.getExplainData(date);
	}
	ZdtjModule.prototype.getExplainData = async function(date) {
		var hour = await clientApi.serverHour();

		if (hour > 0945) {
			this.requestZxData(this.hcType, date);
		} else {
			this.renderRisingProb(this.hcType, '');
		}
	}
	ZdtjModule.prototype.bindEvents = async function() {
		var self = this;
		var tradeDay = await clientApi.getTradingDay();

		var minDate = '2016-01-04';

		this.datePicker(tradeDay, this.domId, function(date) {
			self.requestMinData(date);
		}, function(sdate, edate) {

		}, minDate);

		var hoverTimer;
		$('#' + self.domId + 'ExplainBtn').hover(function() {
			var _this = this;
			hoverTimer = setTimeout(function() {
				$(_this).find('b').show();
				$(_this).parents('.module-item').find('.explain-box').show();
			}, 1000)
		}, function() {
			clearTimeout(hoverTimer);
		});

		$('#' + self.domId + 'ExplainBox').mouseleave(function() {
			$(this).parents('.module-item').find('b.icon').hide();
			$(this).hide();
		});
	}
	ZdtjModule.prototype.analyticalData = async function(result, isUpdate) {
		var self = this;
		var latestVal1 = '--',
			latestVal2 = '--',
			latestVal3 = '--';
		var chartData = [],
			rise5 = '--';
		if (result && result.length > 0) {
			self.sum = eval(result.join("+"));
			var data1 = result[0] + result[1] + result[2];
			var data2 = result[3] + result[4];
			var data3 = result[5] + result[6];
			var data4 = result[7] + result[8] + result[9];

			var data5 = result[10];
			var data6 = result[11] + result[12] + result[13];
			var data7 = result[14] + result[15];
			var data8 = result[16] + result[17];
			var data9 = result[18] + result[19] + result[20];

			latestVal1 = data1 + data2 + data3 + data4;
			latestVal2 = data5;
			latestVal3 = data6 + data7 + data8 + data9;
			chartData = [data1, data2, data3, data4, data5, data6, data7, data8, data9];

			rise5 = data1 + data2;
			$('#' + self.domId + 'ExplainBtn').show();
		} else {
			$('#' + self.domId + 'ExplainBtn').hide();
		}

		self.latestVal1 = latestVal1;
		self.latestVal2 = latestVal2;
		self.latestVal3 = latestVal3;

		self.renderExplain(rise5);

		if (isUpdate && self.HighchartsObj) {
			self.HighchartsObj.series[0].update({
				data: chartData
			});
			$('.zdtjrise-legend').text(latestVal1);
			$('.zdtjpp-legend').text(latestVal2);
			$('.zdtjfall-legend').text(latestVal3);
		} else {
			await self.drawChart(chartData, self.latestVal1, self.latestVal2, self.latestVal3);
		}
	}

	ZdtjModule.prototype.renderExplain = function(data) {
		var self = this;
		if (isNaN(data)) {
			$('#' + self.domId + 'ExplainData').text(self.MODULENAME + '：--');
			return;
		}

		var gradeTxt = '--',
			gradePic = '10';
		if (data >= 300) {
			gradeTxt = '强';
		} else if (data >= 150 && data < 300) {
			gradeTxt = '较强';
		} else if (data >= 80 && data < 150) {
			gradeTxt = '较弱';
		} else if (data >= 0 && data < 80) {
			gradeTxt = '弱';
		}

		if (data <= 40) {
			gradePic = 0;
		} else if (data <= 80) {
			gradePic = 1;
		} else if (data <= 120) {
			gradePic = 2;
		} else if (data <= 150) {
			gradePic = 3;
		} else if (data <= 220) {
			gradePic = 4;
		} else if (data <= 300) {
			gradePic = 5;
		} else if (data <= 500) {
			gradePic = 6;
		} else {
			gradePic = 7;
		}

		$('.' + self.domId + '-explain-pic').css('background-position', -177 * gradePic + 'px 0');
		$('#' + self.domId + 'ExplainData').text(self.MODULENAME + '表现' + gradeTxt);
	}

	ZdtjModule.prototype.drawChart = function(series, data1, data2, data3) {
		var self = this;
		var _xAxisData = ['≥7', '5~7', '3~5', '0~3', '0', '-3~0', '-5~-3', '-7~-5', '≤-7'];

		var chartOptions = {
			credits: {
				enabled: false
			},
			title: {
				text: ''
			},
			chart: {
				backgroundColor: null,
				style: {
					"fontFamily": "Arial, \"Microsoft yahei\", Verdana, Helvetica, sans-serif",
					"fontSize": "12px"
				},
				marginTop: 40,
				marginLeft: 40,
				marginBottom: 30
			},
			series: [{
				name: '涨跌停',
				type: 'column',
				marker: {
					enabled: false
				},
				data: series
			}],

			plotOptions: {
				column: {
					pointWidth: 14,
					borderWidth: 0,
					colorByPoint: true,
					colors: ['red', 'red', 'red', 'red', '#eecb25', 'green', 'green', 'green', 'green']
				},
				series: {
					dataLabels: {
						color: '#808080',
						style: {
							fontWeight: 'normal',
							textShadow: 'null'
						},
						backgroundColor: null,
						shadow: false,
						enabled: true,
						y: 4
					},
					events: {
						legendItemClick: function() {
							return false;
						}
					}
				}
			},
			xAxis: {
				lineWidth: 1,
				tickLength: 4,
				lineColor: '#808080',
				tickColor: '#808080',
				tickInterval: 1,
				title: {
					text: ''
				},
				categories: _xAxisData,
				gridLineWidth: 0,
				labels: {
					align: 'center',
					rotation: 0,
					style: {
						color: '#afafaf'
					}
				},
				min: 0,
				max: 8
			},
			yAxis: {
				allowDecimals: false,
				lineWidth: 1,
				tickColor: '#808080',
				tickPosition: 'outside',
				gridLineWidth: 0,
				tickLength: 4,
				tickWidth: 1,
				title: {
					text: ''
				},
				lineColor: '#808080',
				labels: {
					// format: '{value}%',
					style: {
						color: '#afafaf'
					},
					x: -7
				}
			},
			legend: {
				align: 'right',
				verticalAlign: 'top',
				floating: false,
				symbolHeight: 1,
				symbolWidth: 0,
				itemStyle: {
					color: '#dedede',
					fontFamily: 'Microsoft yahei',
					fontWeight: 'normal',
					cursor: 'default'
				},
				itemHiddenStyle: {},
				itemHoverStyle: {
					color: '#dedede'
				},
				useHTML: true,
				labelFormatter: function() {
					return '<span>上涨<i class="zdtjrise-legend"> ' + data1 + '</i></span>&nbsp;&nbsp;' +
						'<span>平盘<i class="zdtjpp-legend"> ' + data2 + '</i></span>&nbsp;&nbsp;' +
						'<span>下跌<i class="zdtjfall-legend"> ' + data3 + '</i></span>';
				}
			},
			tooltip: {
				backgroundColor: '#2e3339',
				borderColor: '#505458',
				formatter: function() {
					var ratio;
					if (self.sum != 0) {
						ratio = parseFloat(this.y / self.sum * 100).toFixed(2);
					} else {
						ratio = '--';
					}
					return '占当日交易股票的' + ratio + '%';
				},
				style: {
					fontFamily: 'Microsoft yahei',
					color: '#fff'
				}
			}
		};

		$('#' + self.domId + 'ChartBox').highcharts(chartOptions);
		self.HighchartsObj = $('#' + self.domId + 'ChartBox').highcharts();
	}

	ZdtjModule.prototype.updateSeries = function(isHasOverall, date, result) {
		var self = this;
		if (isHasOverall) {
			return;
		}
		var inputDate = $('#'+ self.domId + 'DatePicker').val();
		var isToday = inputDate.replace(/-/g, '') == date;

		if (this.type == 'min' && isToday) {
			if (result) {
				self.analyticalData(result, 'isUpdate');
				// self.getExplainData(date);
			} else {
				self.requestMinData(date);
				// self.getExplainData(date);
			}

		}
	}

	module.exports = ZdtjModule;
});